package com.qingge.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qingge.springboot.common.Result;
import com.qingge.springboot.entity.*;
import com.qingge.springboot.service.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/paper")
public class PaperController {

    @Resource
    private IPaperService paperService;
    
    @Resource
    private IClassCourseTeacherService classCourseTeacherService;

    @Resource
    private ICourseNewService courseService;

    @Resource
    private RedisTemplate redisTemplate;

//    @GetMapping("/{ctId}")
//    @ApiOperation("通过课程和教师id 查询试卷信息")
//    public Result getPaper(@PathVariable String ctId){
//        LambdaQueryWrapper<Paper> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(Paper::getCtId,ctId);
//        List<Paper> papers = paperService.getBaseMapper().selectList(queryWrapper);
//        return Result.success(papers);
//    }

    @GetMapping
    public Result getPaper(@RequestParam Integer pageSize,@RequestParam Integer pageNum,@RequestParam(required = false) String title){
        LambdaQueryWrapper<Paper> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasLength(title),Paper::getPaperName,title);
        return Result.success(paperService.page(new Page<>(pageSize,pageNum), queryWrapper));
    }

    @GetMapping("/startTest/{courseId}/{paperId}")
    public Result startTest(@PathVariable String paperId, @PathVariable String courseId){
        //给当前试卷赋值考试科目
        //获取科目名称
        CourseNew course = courseService.getById(courseId);
        LambdaUpdateWrapper<Paper> set = new LambdaUpdateWrapper<Paper>().set(Paper::getCourseId, courseId).set(Paper::getCourseName, course.getCourseName()).eq(Paper::getId, paperId).set(Paper::getIsStart, 1).set(Paper::getDateTime, LocalDateTime.now());
        boolean update = paperService.update(set);
        //将数据存入redis当中 倒计时考试考试
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set(paperId,"During",120, TimeUnit.MINUTES);
        return Result.success();
    }

    @GetMapping("/getCourseByCourseId/{courseId}")
    public Result getPaperByCourseId(@PathVariable String courseId){
        LambdaQueryWrapper<Paper> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Paper::getCourseId,courseId);
        List<Paper> papers = paperService.getBaseMapper().selectList(queryWrapper);
        return Result.success(papers);
    }

    @PostMapping
    public Result save(@RequestBody Paper paper){
        paper.setIsStart(0);
        paper.setScore(120);
        paperService.save(paper);
        return Result.success();
    }


}
